package com.recharge.controller.callback;

import com.recharge.OrderService;
import com.recharge.domain.TradeFlow;
import com.recharge.domain.TradeOrder;
import com.recharge.mapper.ITradeFlowMapper;
import com.recharge.mapper.ITradeOrderMapper;
import com.recharge.sup.AbsSupChargeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by CAOQI on 2016/12/17.
 */
@Controller
@RequestMapping("/yunllhf")
public class YunllTaiWeiCallBackController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ITradeOrderMapper iTradeOrderMapper;

    @Autowired
    private ITradeFlowMapper iTradeFlowMapper;

    @Autowired
    private OrderService orderService;

    @Resource(name = "yunLiuLiangChargeServiceImpl")
    private AbsSupChargeService absSupChargeService;

    @RequestMapping("/callback")
    @ResponseBody
    public String yunLiuLiang(@RequestParam("merchant_id")String merchant_id,
                              @RequestParam("merchant_orderid")String merchant_orderid,
                              @RequestParam("order_id")String order_id,
                              @RequestParam("charge_status")String charge_status,
                              @RequestParam("sign")String sign){
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("merchant_id", merchant_id);
        paramMap.put("merchant_orderid", merchant_orderid);
        paramMap.put("order_id", order_id);
        paramMap.put("charge_status", charge_status);
        paramMap.put("sign", sign);
        
        logger.info("orderid:{},收到结果回调,status:{}",order_id,charge_status);

        try {
            TradeFlow tradeFlow = iTradeFlowMapper.selectBySerId(order_id);
            TradeOrder tradeOrder =iTradeOrderMapper.selectByOrderId(tradeFlow.getOrderId());
            absSupChargeService.response(tradeOrder,tradeFlow,paramMap);
        } catch (Exception e) {
            logger.error("回调出现异常",e);
        }

        return "ok";
    }
}
